import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:note/common/common.dart';
import 'package:note/common/tools.dart';
import 'package:note/widgets/BasePage.dart';
import 'package:note/widgets/markdown.dart';
import 'package:note/widgets/scrollBarView.dart';
import 'package:note/widgets/showPhotoView.dart';

import 'logic.dart';

class NoteInfoPage extends StatelessWidget {
  NoteInfoPage({Key? key}) : super(key: key);

  final logic = Get.put(NoteInfoPageLogic());
  final state = Get.find<NoteInfoPageLogic>().state;

  @override
  Widget build(BuildContext context) {
    return BasePage(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Styles.backgroundColor,
          foregroundColor: Styles.mainThemeColor,
          title: Obx(() {
            return Text(
              state.note.value.title,
              maxLines: 1,
            );
          }),
          actions: [
            IconButton(
                onPressed: logic.onLock,
                icon: Obx(() => Icon(
                      state.note.value.isLocked
                          ? Icons.lock_outline_rounded
                          : Icons.lock_open,
                    ))),
            IconButton(
                onPressed: logic.onFavorite,
                icon: Obx(() => Icon(
                      state.note.value.isFavorite
                          ? Icons.favorite
                          : Icons.favorite_border,
                      color: Styles.favorite,
                    ))),
            IconButton(
                onPressed: logic.toDelete,
                icon: const Icon(Icons.delete_forever)),
            IconButton(
                onPressed: logic.onExport, icon: const Icon(Icons.download))
          ],
        ),
        body: Obx(() {
          return DefaultScrollBarView(
            controller: state.scrollController,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //图片
                Obx(() {
                  if (state.note.value.images.isEmpty) {
                    return Container();
                  }
                  return Container(
                    height: 200,
                    color: Styles.backgroundColor,
                    child: Swiper(
                      itemCount: state.note.value.images.length,
                      controller: state.swiperController,
                      loop: false,
                      pagination: const SwiperPagination(
                          builder: DotSwiperPaginationBuilder(
                              size: 6,
                              activeSize: 12,
                              color: Styles.backgroundColor,
                              activeColor: Styles.mainThemeColor)),
                      itemBuilder: (BuildContext context, int index) {
                        return Image(
                          image:
                              FileImage(File(state.note.value.images[index])),
                        );
                      },
                      onTap: (index) {
                        Get.to(ShowPhotoGallery(
                          index: index,
                          imagePaths: state.note.value.images,
                        ))?.then((value) {
                          state.swiperController.move(value, animation: false);
                        });
                      },
                      onIndexChanged: (index) {
                        state.swiperController.index = index;
                      },
                    ),
                  );
                }),
                //标题
                Padding(
                  padding: const EdgeInsets.only(left: 16.0, top: 10),
                  child: Text(
                    state.note.value.title,
                    style: Styles.textStyleMedium(30),
                  ),
                ),
                //内容
                MyMarkDownBody(
                  data: state.note.value.data,
                  padding: const EdgeInsets.fromLTRB(15, 8, 15, 8),
                ),
                //时间
                Align(
                    alignment: Alignment.centerRight,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        DateTimeToString.toYYMMDDHSN(state.note.value.editTime),
                        style: Styles.textTransparentNormal(15),
                      ),
                    )),
              ],
            ),
          );
        }),
        floatingActionButton: Container(
          width: 50,
          height: 50,
          margin: const EdgeInsets.only(right: 10, bottom: 15),
          decoration: const BoxDecoration(
              color: Styles.mainThemeColor,
              borderRadius: BorderRadius.all(Radius.circular(30)),
              boxShadow: [
                BoxShadow(
                    color: Styles.shadowColor,
                    blurRadius: 10,
                    offset: Offset(0, 5))
              ]),
          child: IconButton(
              onPressed: logic.toEdit,
              icon: const Icon(
                Icons.edit_calendar,
                color: Styles.lightColor,
                size: 27,
              )),
        ),
      ),
    );
  }
}
